package com.karaku.karaku_collector.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.karaku.karaku_collector.LocalSetting;
import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.util.actions.Action1;

/**
 * Created by guoping on 2017/11/1.
 */

@BindView(R.layout.view_scale)
public class ScaleView extends FrameLayout {

    @BindView(R.id.seek_bar)
    private SeekBar seekBar;
    @BindView(R.id.tv_scale)
    private TextView tvScale;
    @BindView(R.id.view_content)
    private View contentView;

    private Action1<Float> scaleChangedCallback;

    public ScaleView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this ,this, true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }


    public void show(final Action1<Float> scaleChangedCallback) {
        this.scaleChangedCallback = scaleChangedCallback;
        updateTextView();
        seekBar.setProgress((int)(LocalSetting.getInstance().getScale() * 100));
        setVisibility(VISIBLE);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    float scale = progress * 1.f / 100;
                    LocalSetting.getInstance().setScale(scale);
                    updateTextView();
                    scaleChangedCallback.run(scale);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public void hide() {
        this.scaleChangedCallback = null;
        setVisibility(GONE);
    }

    public void setContentLayoutParams(LayoutParams params) {
        contentView.setLayoutParams(params);
    }

    private void updateTextView() {
        tvScale.setText(String.format("%.2f", LocalSetting.getInstance().getScale()));
    }

}
